using System;
namespace 简单工厂
{
    class Program
    {//形状抽象类（父类）
        abstract class Shape
        {
            public abstract double Area();         //面积           
            public abstract double Perimeter(); //周长       
        }
        class Circle : Shape
        {
            double radius;
            public Circle(double r)
            {
                this.radius = r;
            }

            public override double Area()
            {
                return Math.PI * Math.Pow(radius, 2);
            }

            public override double Perimeter()
            {
                return 2 * Math.PI * radius;
            }
        }
        //长方形
        class Rectangle : Shape
        {

            double width;
            double height;
            public Rectangle(double w,double h)
            {
                this.width = w;
                this.height = h;
            }

            public override double Area()
            {
                return width * height;
            }

            public override double Perimeter()
            {
                return 2 * (width + height);
            }
        }

        //正方形
        class Square : Shape
        {
            double side;
            public Square(double s)
            {
                this.side = s;
            }
            public override double Area()
            {
                return Math.Pow(side, 2);
            }

            public override double Perimeter()
            {
                return 4 * side;
            }
        }

        //三角形
        class Triangle : Shape
        {
            double side1;
            double side2;
            double side3;
            public Triangle(double a1,double a2,double a3)
            {
                this.side1 = a1;
                this.side2 = a2;
                this.side3 = a3;
            }

            public override double Area()
            {

                return Math.Sqrt((side1 + side2 + side3) * (side1 + side2 - side3) * (side1 + side3 - side2) * (side2 + side3 - side1)) / 4;
            }

            public override double Perimeter()
            {

                return side1 + side2 + side3;
            }

        }

         class ShapeFactory
        {
            //随机生成对象
            public  Shape Createshape()
            {
                Shape sp = null;
                Random rd;
                rd = new Random();
                int j;
                j = rd.Next(0,4);
                if (j == 0)
                {
                    sp = new Circle(rd.Next(1,10));
                }
                else if (j == 1)
                {
                    sp = new Rectangle(rd.Next(1,10), rd.Next(1,10));
                }
                else if(j==2)
                {
                    sp = new Square(rd.Next(1,10));
                }
                else if (j == 3)
                {
                    double a, b, c;
                    a = rd.Next(1,10);
                    c = b = a;//简化，以防生成奇怪的图形
                    sp = new Triangle(a, b, c);
                }
                return sp;

            }
        }


        static void Main(string[] args)
        {
            ShapeFactory ot = new ShapeFactory();
            Shape[] s = new Shape[10];
            double sum = 0;
            for (int i = 0; i < 10; i++)
            {
                s[i] = ot.Createshape();
            }
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("创建的第{0}图形是",i+1);
                if (s[i] is Circle ) Console.WriteLine("圆形");
                else if(s[i] is Rectangle) Console.WriteLine("长方形");
                else if (s[i] is Square) Console.WriteLine("正方形");
                else if (s[i] is Triangle) Console.WriteLine("三角形");
                Console.WriteLine("面积为：" + String.Format("{0}", s[i].Area())
                + "周长为：" + String.Format("{0}", s[i].Perimeter()));
            }
            for (int i = 0; i < 10; i++)
            {
               sum += s[i].Area();
            }
            Console.WriteLine("面积之和为{0}：", sum);
        }
    }
}